package ar.edu.utn.frsf.cidisi.upcolbpip.modelgeneration.generator;

import java.io.IOException;
import java.util.HashMap;
import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import ar.edu.utn.frsf.cidisi.upcolbpip.UPColBPIPModel;
import ar.edu.utn.frsf.cidisi.upcolbpip.presentation.UpcolbpipEditorPlugin;

/**
 * @generated
 */
public class CompleteInteractionProtocolsAction implements IObjectActionDelegate {

	private static String TARGETDIR = "";

	/**
	 * @generated
	 */
	private IWorkbenchPart targetPart;

	/**
	 * @generated
	 */
	private URI domainModelURI;

	/**
	 * @generated
	 */
	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		this.targetPart = targetPart;
	}

	/**
	 * @generated
	 */
	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		domainModelURI = null;
		action.setEnabled(false);
		if (selection instanceof IStructuredSelection == false
				|| selection.isEmpty()) {
			return;
		}
		IFile file = (IFile) ((IStructuredSelection) selection)
				.getFirstElement();
		domainModelURI = URI.createPlatformResourceURI(file.getFullPath()
				.toString(), true);
		action.setEnabled(true);
	}

	/**
	 * @generated
	 */
	private Shell getShell() {
		return targetPart.getSite().getShell();
	}

	/**
	 * @generated
	 */
	@Override
	public void run(IAction action) {
		
		
		EObject diagramRoot = null;
		ResourceSet resourceSet = null;
		Resource resource = null;		
		
		try {
			// Create a resource set
			//
			resourceSet = new ResourceSetImpl();

			resource = resourceSet.getResource(domainModelURI, true);
			diagramRoot = resource.getContents().get(0);
		} catch (WrappedException ex) {
			UpcolbpipEditorPlugin.INSTANCE.getPluginLogger().log(
					"Unable to load resource: " + domainModelURI + "\n" + ex); //$NON-NLS-1$
		}
		if (diagramRoot == null) {
//			MessageDialog
//					.openError(
//							getShell(),
//							Messages.UpcolbpipInitDiagramFileAction_InitDiagramFileResourceErrorDialogTitle,
//							Messages.UpcolbpipInitDiagramFileAction_InitDiagramFileResourceErrorDialogMessage);
			return;
		}
		
		if (diagramRoot instanceof UPColBPIPModel){
			
			try {

		        System.out.println();
		        System.out.println();
				System.out.println("*****************************************************************");
				System.out.println("*                      Starting Completion                      *");
				System.out.println("*****************************************************************");
				
				
				Experiment experiment = new Experiment();
				
				experiment.setMinSequence(1);
				experiment.setMaxSequence(5);
				experiment.setMinInteractionPath(2);
				experiment.setMaxInteractionPath(5);
				experiment.setMinHandlers(1);
				experiment.setMaxHandlers(3);
				experiment.setMinNodes(experiment.getMaxSequence());
				experiment.setMaxNodes(20);
				experiment.setMinNestingLevel(3);
				experiment.setMaxNestingLevel(8);
				
				experiment.setMaxInteractionProtocols(10);
				
//				experiment.setElementProbability(Experiment.SIMPLE_ELEMENTS);
				experiment.setElementProbability(Experiment.FULL_ELEMENTS);
				
				CompleteModel model = new CompleteModel((UPColBPIPModel)diagramRoot,experiment);
				
//		        System.out.print("\n");
//				System.out.println("PNC Id"+"\t"+"Nodes Count"+"\t"+"Arc count"+"\t"+
//						"And"+"\t"+"Xor"+"\t"+"Or"+"\t"+"While"+"\t"+"Until"+"\t"+"Exc"+
//						"\t"+"Cancel"+"\t"+"MI"+"\t"+"Term"+"\t"+"BM"+"\t"+"Generation Time [ms]");
				model.completeInteractionProtocols(); 

				
				System.out.print("\n");
				System.out.println("*****************************************************************");
				System.out.println("*                        Ending Completion                      *");
				System.out.println("*****************************************************************");
	
//				MessageDialog
//				.openInformation(
//						getShell(),
//						"UP-ColBPIP",
//						"Petri Net specification was generated on " + ip.getName());
				
			} catch (NullPointerException e1) {
				System.out.println("An error was found during the generation of interaction protocols");
				e1.printStackTrace();
			}

			try {
				
				resource.save(new HashMap<Object, Object>());
				
			} catch (IOException e) {

			}

		}
		
	}
	
	
}
